package com.meihua.baoleyi.adapters;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.bean.AddressListBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.dialog.ClearCacheDialog;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.ui.AddAdressActivity;
import com.meihua.baoleyi.ui.AddressActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.TreeMap;

import okhttp3.Call;
import okhttp3.Response;

import static com.meihua.baoleyi.R.id.identity_default_address;

/**
 * Created by liming on 2017/10/17
 * 地址管理适配器
 */

public class AddressMessageAdapter extends BaseAdapter {

    private LayoutInflater lif;
    private AddressActivity activity;
    private List<AddressListBean.DatasBean> datasBeanList;
    private AddressListBean.DatasBean datasBean;
    private CheckBox currentCheckbox;

    public AddressMessageAdapter(AddressActivity activity, List<AddressListBean.DatasBean> datasBeanList) {
        this.datasBeanList = datasBeanList;
        this.activity = activity;
        lif = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        return datasBeanList == null ? 0 : datasBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = lif.inflate(R.layout.item_identity_manager, viewGroup,false);
            holder.identity_item_address = (TextView) view.findViewById(R.id.identity_item_address);
            holder.identity_item_name = (TextView) view.findViewById(R.id.identity_item_name);
            holder.identity_item_phone = (TextView) view.findViewById(R.id.identity_item_phone);
            holder.change_ll = (LinearLayout) view.findViewById(R.id.change_ll);
            holder.delete_ll = (LinearLayout) view.findViewById(R.id.delete_ll);
            holder.identity_default_address = (CheckBox) view.findViewById(identity_default_address);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //设置数据
        datasBean = datasBeanList.get(i);
        holder.identity_item_name.setText(datasBean.getConsignee());
        holder.identity_item_phone.setText(datasBean.getPhone());
        holder.identity_item_address.setText(datasBean.getProvince() + datasBean.getCity() + datasBean.getArea() + datasBean.getAddress());
        if (datasBean.getIs_default() == 1) {
            holder.identity_default_address.setChecked(true);
            currentCheckbox = holder.identity_default_address;
            holder.identity_default_address.setText("默认地址");
        } else {
            holder.identity_default_address.setChecked(false);
            holder.identity_default_address.setText("设为默认");
        }
        holder.identity_default_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果当前点击是默认地址 不让点击
                if (datasBeanList.get(i).getIs_default() == 1) {
                    return;
                }
                setDefault(i);
            }
        });
        //编辑
        holder.change_ll.setOnClickListener(new ClickListener(i));
        //删除
        holder.delete_ll.setOnClickListener(new ClickListener(i));

        return view;
    }

    /**
     * 点击事件
     */
    private class ClickListener implements View.OnClickListener{

        private int position;

        public ClickListener(int i) {
            position = i;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.delete_ll://删除
                    ClearCacheDialog dlg = new ClearCacheDialog.Builder(activity)
                            .setMessage("确定要删除地址吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    deleteAddress(position);
                                    dialogInterface.dismiss();
                                }
                            })
                            .setNativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            })
                            .build();

                    dlg.show();
                    break;
                case R.id.change_ll://编辑
                    Intent in = new Intent(activity, AddAdressActivity.class);
                    in.putExtra("addressbean", datasBeanList.get(position));
                    activity.startActivityForResult(in, 2);
                    break;
            }
        }
    }

    /**
     * ViewHolder减少findViewById使用
     */
    private static class ViewHolder {
        public TextView identity_item_name;//用户名
        public TextView identity_item_phone;//手机号
        public TextView identity_item_address;//详细地址
        public LinearLayout change_ll;//编辑
        public LinearLayout delete_ll;//删除
        public CheckBox identity_default_address;//默认地址
    }

    /**
     * 设置默认
     */
    public void setDefault(int position) {
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        treeMap.put("user_id", MyApp.getInstance().getUser_id());
        treeMap.put("id", datasBeanList.get(position).getId() + "");
        NetHelper.get(APIServer.SETDEFAULT, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                JSONObject object = null;
                try {
                    object = new JSONObject(s);
                    if (object.getString("code").equals("200")) {
                        activity.getServerData();
                        Toast.makeText(activity, "设置成功", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 删除地址
     * @param position
     */
    private void deleteAddress(int position) {
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        treeMap.put("id", datasBeanList.get(position).getId() + "");//地址记录id
        NetHelper.get(APIServer.ADDRESSDEL, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                JSONObject object = null;
                try {
                    object = new JSONObject(s);
                    if (object.getString("code").equals("200")) {
                        activity.getServerData();
                        Toast.makeText(activity, "删除成功", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
